<?php
/*首页数据*/
namespace Home\Model;
use Think\Model;
class IndexModel extends Model {
    const SHOW_HOT_COUNT=6;
    const SHOW_NEW_COUNT=6;
    const SHOW_PROMO_COUNT=10;
    const SHOW_PAGE_COUNT=5;
    public function getCarData($cityName){
        //获取城市
        $city=M('city');
        $where="name='".$cityName."' and is_show=1";
        $field="id";
        $city_info=$city->field($field)->where($where)->find();
        $carnums='';

        $car=M('qcgl_manage', null);
        //获取促销车型
        $where="iscxcx = 1";
        $order="iscxcx asc";
        $field="id, qcmc, jb, xsjg, cs, cszdj, yg";
        $promo_car=$car->field($field)->where($where)->order($order)->limit(0,self::SHOW_PROMO_COUNT)->select();
        foreach($promo_car as $val){
            $carnums.=$carnums?','.$val['id']:$val['id'];
        }
        //获取热销车型
        $where="ishdcx = 1";
        $order="ishdcx asc";
        $field="id, qcmc, jb, xsjg, cs, cszdj, yg";
        $hot_car=$car->field($field)->where($where)->order($order)->limit(0,self::SHOW_HOT_COUNT)->select();
        foreach($hot_car as $val){
            $carnums.=$carnums?','.$val['id']:$val['id'];
        }
        //获取新品车型
        $where="isxctj = 1";
        $order="isxctj asc";
        $field="id, qcmc, jb, xsjg, cs, cszdj, yg";
        $new_car=$car->field($field)->where($where)->order($order)->limit(0,self::SHOW_NEW_COUNT)->select();
        foreach($new_car as $val){
            $carnums.=$carnums?','.$val['id']:$val['id'];
        }
        //获取图片
        $carImageDb = M('qcgl_tp', null);
        $where = "qcid in (".$carnums.") and type = 162";
        $carImages = $carImageDb -> where($where) -> order('pxh asc') -> select();
        // var_dump($carImageDb -> getLastSql());
        $images = [];
        foreach ($carImages as $k => $v) {
            if (!key_exists($v['qcid'], $images)) {
                $images[$v['qcid']] = $v['tpmc'];
            }
        }
        foreach ($promo_car as $k => $v) {
            $promo_car[$k]['image'] = $images[$v['id']];
        }
        foreach ($hot_car as $k => $v) {
            $hot_car[$k]['image'] = $images[$v['id']];
        }
        foreach ($new_car as $k => $v) {
            $new_car[$k]['image'] = $images[$v['id']];
        }
        $data=array();
        $data['promo_car']=$promo_car;
        $data['hot_car']=$hot_car;
        $data['new_car']=$new_car;

        $car_baike = M('car_baike');
        $baike_res = $car_baike->field('id,title') -> where('is_show=1') ->limit(0,5)-> select();
        $data['car_baike']=$baike_res;

        $broadcast = M('broadcast');
        $broadcast_res = $broadcast->field('id,tel,type')-> select();
        shuffle($broadcast_res);
        $data['broadcast']=$broadcast_res;
        
        return $data;
    }
    public function getDetailsDate($carnum){
         $details = M('car_details');      
         $where="carnum=".$carnum;
         $field=" * ";
         $base=$details->field($field)->where($where)->find();
         return $base;

    }  
    public function getCarPrice($carnum,$cityid){
        

         $price = M('price');      
         $where="carnum=".$carnum." and is_new=1 and city_id=".$cityid;
         $order="purchase_scheme asc";
         $field=" * ";
         $base=$price->field($field)->where($where)->select();
         return $base;

    }
    public function getCarsDate($carnum, $hyid){
        $cars = M('qcgl_manage', null);      
        $where="id=".$carnum;
        $field=" * ";
        $detail=$cars->field($field)->where($where)->find();

        $data = [];

        $carImageDb = M('qcgl_tp', null);
        $images = $carImageDb -> where(['qcid' => $detail['id']]) -> order('type asc, pxh') -> select();
        $data['images'] = $images;

        $gcDb = M('gcys_manage', null);
        $gcys = $gcDb -> where(['id' => 1]) -> find();
        $data['gcys'] = $gcys['gcys'];
        
        $gmDb = M('gmxz_manage', null);
        $gmxz = $gmDb -> field('xzmc, xznr') -> select();
        $data['gmxz'] = $gmxz;

        $mdDb = M('mdgl_manage', null);
        $mdgl = $mdDb  -> field('mdmc, address, lxdh, lng, lat') -> limit(0, 3) -> select();
        $data['mdgl'] = $mdgl;

        $llDb = M('interest_manage', null);
        $ll = $llDb -> field('id, type, typename, oneyear, twoyear, threeyear') -> select();
        $data['ll'] = $ll;

        $fwDb = M('fwf_manage', null);
        $fwf = $fwDb -> where(['id' => 1]) -> find();
        $data['fwf'] = $fwf['fwf'];
        
        $collectionDb = M('wdsc_manage', null);
        $collection = $collectionDb -> where(['qcid' => $carnum, 'hyid' => $hyid]) -> find();
        if ($collection) {
            $data['collection'] = 1;
        } else {
            $data['collection'] = 0;
        }
        $typeDb = M('s_sjzd', null);
        $types = $typeDb -> where(['type' => '车辆级别']) -> select();
        $typeData = [];
        foreach ($types as $k => $v) {
            $typeData[$v['id']] = $v['content'];
        }
        $base_info = [];
        $base_info[] = ['厂商', $detail['cs']];
        $base_info[] = ['级别', $typeData[$detail['jb']]];
        $base_info[] = ['发动机', $detail['fdj']];
        $base_info[] = ['变速箱', $detail['bsx']];
        $base_info[] = ['长 * 宽 * 高(mm)', $detail['ckg']];
        $base_info[] = ['车身型式', $detail['csxs']];
        $base_info[] = ['最高速度（km/h)', $detail['zgsd']];
        $base_info[] = ['燃料形式', $detail['rlxs']];
        $base_info[] = ['工信部综合油耗（L/100km)', $detail['gxbzhyh']];
        $base_info[] = ['外部颜色', $detail['wbys']];
        $base_info[] = ['内饰颜色', $detail['nsys']];
        $base_info[] = ['能源类型', $detail['energyType']];
        $base_info[] = ['工信部纯电续驶里程(km)', $detail['energyLetterKm']];
        $base_info[] = ['快充时间(小时)', $detail['energyKTime']];
        $base_info[] = ['慢充时间(小时)', $detail['energyMTime']];
        $base_info[] = ['实测续航里程(km)', $detail['energyXKm']];
        $base_info[] = ['实测快充时间(km)', $detail['energySKTime']];
        $base_info[] = ['实测慢充时间(km)', $detail['energySMTime']];
        $data['base_info'] = $base_info;

        $motor_config = [];
        $motor_config[] = ['电机类型', $detail['motorType']];
        $motor_config[] = ['电动机总功率(kW)', $detail['motorTotalKw']];
        $motor_config[] = ['电动机总扭矩(N.m)', $detail['motorTotalNm']];
        $motor_config[] = ['前电动机最大功率(kW)', $detail['beforeMotorMaxKw']];
        $motor_config[] = ['前电动机最大扭矩(N.m)', $detail['beforeMotorMaxNm']];
        $motor_config[] = ['后电动机最大功率(kW)', $detail['afterMotorMaxKw']];
        $motor_config[] = ['后电动机最大扭矩(N.m)', $detail['afterMotorMaxNm']];
        $motor_config[] = ['驱动电机数', $detail['motorNum']];
        $motor_config[] = ['电机布局', $detail['motorLayout']];
        $motor_config[] = ['电池类型', $detail['batteryType']];
        $motor_config[] = ['工信部续航里程(km)', $detail['enduranceKm']];
        $motor_config[] = ['电池容量(kWh)', $detail['batteryKwk']];
        $motor_config[] = ['电池组质保', $detail['batteryWarranty']];
        $motor_config[] = ['快充电量(%)', $detail['kElectricity']];
        $data['motor_config'] = $motor_config;

        $body_config = [];
        $body_config[] = ['长 * 宽 * 高(mm)', $detail['ckg']];
        $body_config[] = ['轴距(mm)', $detail['zj']];
        $body_config[] = ['前轮距（mm）', $detail['qlj']];
        $body_config[] = ['后轮距（mm）', $detail['hlj']];
        $body_config[] = ['最小离地间隙（mm）', $detail['zxldjx']];
        $body_config[] = ['整车质量（kg）', $detail['zczl']];
        $body_config[] = ['车身结构', $detail['csjg']];
        $body_config[] = ['油箱容积(L)', $detail['yxrj']];
        $body_config[] = ['行李厢容积（L）', $detail['xlxrj']];
        $data['body_config'] = $body_config;

        $engine_config = [];
        $engine_config[] = ['发动机型号', $detail['fdjxh']];
        $engine_config[] = ['排量', $detail['pl']];
        $engine_config[] = ['进气形式', $detail['jqxs']];
        $engine_config[] = ['气缸排列形式', $detail['qgplxs']];
        $engine_config[] = ['气缸数（个）', $detail['qgs']];
        $engine_config[] = ['每缸气门数（个）', $detail['mgqms']];
        $engine_config[] = ['配气机构', $detail['pqjg']];
        $engine_config[] = ['缸径（mm）', $detail['gj']];
        $engine_config[] = ['最大马力（Ps）', $detail['zdml']];
        $engine_config[] = ['最大功率（kW）', $detail['zdgl']];
        $engine_config[] = ['最大功率转速（rpm）', $detail['zdglzs']];
        $engine_config[] = ['最大扭转（N·m）', $detail['zdnz']];
        $engine_config[] = ['最大扭转转速（rpm）', $detail['zdnzzs']];
        $engine_config[] = ['发动机特有技术', $detail['fdjtyjs']];
        $engine_config[] = ['燃料形式', $detail['rlxs']];
        $engine_config[] = ['燃油标号', $detail['rybh']];
        $engine_config[] = ['供油方式', $detail['gyfs']];
        $engine_config[] = ['缸盖材料', $detail['ggcl']];
        $engine_config[] = ['缸体材料', $detail['gtcl']];
        $engine_config[] = ['环保标准', $detail['hbbz']];
        $data['engine_config'] = $engine_config;

        $gearbox_config = [];
        $gearbox_config[] = ['挡位个数', $detail['dwgs']];
        $gearbox_config[] = ['变速箱类型', $detail['bsxlx']];
        $gearbox_config[] = ['变速箱描述', $detail['bsxms']];
        $data['gearbox_config'] = $gearbox_config;

        $chassis_steering = [];
        $chassis_steering[] = ['驱动方式', $detail['qdfs']];
        $chassis_steering[] = ['前悬架类型', $detail['qxclx']];
        $chassis_steering[] = ['后悬架类型', $detail['hxclx']];
        $chassis_steering[] = ['助力类型', $detail['zlfs']];
        $chassis_steering[] = ['车体结构', $detail['ctjg']];
        $data['chassis_steering'] = $chassis_steering;

        $wheel_brake = [];
        $wheel_brake[] = ['前制动器类型', $detail['qzdqlx']];
        $wheel_brake[] = ['后制动器类型', $detail['hzdqlx']];
        $wheel_brake[] = ['驻车制动类型', $detail['zczdlx']];
        $wheel_brake[] = ['前轮胎规格', $detail['qltgg']];
        $wheel_brake[] = ['后轮胎规格', $detail['hltgg']];
        $wheel_brake[] = ['备胎规格', $detail['btgg']];
        $data['wheel_brake'] = $wheel_brake;

        $safety_equip = [];
        $safety_equip[] = ['主/副驾驶座安全气囊', $detail['zjszaqqn']];
        $safety_equip[] = ['前/后排侧气囊', $detail['qpcqn']];
        $safety_equip[] = ['前/后排头部气囊（气帘）', $detail['qptbqnql']];
        $safety_equip[] = ['膝部气囊', $detail['xbqn']];
        $safety_equip[] = ['胎压监测装置', $detail['tyjczz']];
        $safety_equip[] = ['零胎压继续行驶', $detail['ltyjxxs']];
        $safety_equip[] = ['安全带未系提示', $detail['aqdwxts']];
        $safety_equip[] = ['ISOFIX儿童座椅接口', $detail['isofixetzyjk']];
        $safety_equip[] = ['ABS防抱死', $detail['absfbs']];
        $safety_equip[] = ['制动力分配（EBD/CBC等)', $detail['zdlfp']];
        $safety_equip[] = ['刹车辅助（EBA/BAS/BA等）', $detail['scfz']];
        $safety_equip[] = ['牵引力控制（ASR/TCS/TRC)', $detail['qylkz']];
        $safety_equip[] = ['车身稳定控制（ESC/ESP/DSC)', $detail['cswdkz']];
        $safety_equip[] = ['并线辅助', $detail['bxfz']];
        $data['safety_equip'] = $safety_equip;

        $control_config = [];
        $control_config[] = ['前/后驻车雷达 ', $detail['qzcld']];
        $control_config[] = ['倒车视频影像 ', $detail['dcspyx']];
        $control_config[] = ['全景摄像头 ', $detail['qjsxt']];
        $control_config[] = ['定速巡航 ', $detail['dsxh']];
        $control_config[] = ['自适应巡航 ', $detail['zsyxh']];
        $control_config[] = ['自动泊车入位 ', $detail['zdbcrw']];
        $control_config[] = ['发动机启停技术 ', $detail['fdjqtjs']];
        $control_config[] = ['上坡辅助 ', $detail['spfz']];
        $control_config[] = ['自动驻车 ', $detail['zdzc']];
        $control_config[] = ['陡坡缓降 ', $detail['dphj']];
        $control_config[] = ['可变悬架 ', $detail['kbxj']];
        $control_config[] = ['空气悬架 ', $detail['kqxj']];
        $control_config[] = ['可变转向比 ', $detail['kbzxb']];
        $control_config[] = ['整体主动转向系统', $detail['ztzdzxxt']];
        $control_config[] = ['整体主动转向系统', $detail['ztzdzxxt']];
        $data['control_config'] = $control_config;

        $outside_config = [];
        $outside_config[] = ['电动天窗', $detail['ddtc']];
        $outside_config[] = ['全景天窗', $detail['qjtc']];
        $outside_config[] = ['运动外观套件', $detail['ydwgtj']];
        $outside_config[] = ['铝合金轮圈', $detail['lhjlq']];
        $outside_config[] = ['电动吸合门', $detail['ddxhm']];
        $outside_config[] = ['侧滑门', $detail['chm']];
        $outside_config[] = ['电动后备箱', $detail['ddhbx']];
        $outside_config[] = ['感应后备箱', $detail['gyhbx']];
        $outside_config[] = ['车顶行李架', $detail['cdxlj']];
        $outside_config[] = ['发动机电子防盗', $detail['fdjdzfd']];
        $outside_config[] = ['车内中控锁', $detail['cnzks']];
        $outside_config[] = ['遥控钥匙', $detail['ykys']];
        $outside_config[] = ['无钥匙启动系统', $detail['wysqdxt']];
        $outside_config[] = ['无钥匙进入系统', $detail['wysjrxt']];
        $data['outside_config'] = $outside_config;

        $inside_config = [];
        $inside_config[] = ['真皮方向盘', $detail['zpfxp']];
        $inside_config[] = ['方向盘调节', $detail['fxptj']];
        $inside_config[] = ['方向盘电动调节', $detail['fxpddtj']];
        $inside_config[] = ['多功能方向盘', $detail['dgnfxp']];
        $inside_config[] = ['方向盘换挡', $detail['fxphd']];
        $inside_config[] = ['方向盘加热', $detail['fxpjr']];
        $inside_config[] = ['方向盘记忆', $detail['fxpjy']];
        $inside_config[] = ['行车电脑显示屏', $detail['xcdnxsp']];
        $data['inside_config'] = $inside_config;

        $chair_config = [];
        $chair_config[] = ['座椅材质', $detail['zycz']];
        $chair_config[] = ['运动风格座椅', $detail['ydfgzy']];
        $chair_config[] = ['座椅高低调节', $detail['zygdtj']];
        $chair_config[] = ['腰部支撑调节', $detail['ybzctj']];
        $chair_config[] = ['肩部支撑调节', $detail['jbzctj']];
        $chair_config[] = ['主驾驶座电动调节', $detail['zjszddtj']];
        $chair_config[] = ['副驾驶座电动调节', $detail['fjszddtj']];
        $chair_config[] = ['后排座椅电动调节', $detail['hpzyddtj']];
        $data['chair_config'] = $chair_config;

        $multimedia_config = [];
        $multimedia_config[] = ['GPS导航系统', $detail['gpsdhxt']];
        $multimedia_config[] = ['定位互动服务', $detail['dwhdfw']];
        $multimedia_config[] = ['中控台彩色大屏', $detail['zktcsdp']];
        $multimedia_config[] = ['蓝牙/车载电话', $detail['lyczdh']];
        $multimedia_config[] = ['外接音源接口', $detail['wjyyjk']];
        $multimedia_config[] = ['多媒体系统（CD/DVD）', $detail['dmtxt']];
        $multimedia_config[] = ['扬声器数量', $detail['ysqsl']];
        $data['multimedia_config'] = $multimedia_config;

        $light_config = [];
        $light_config[] = ['远光灯', $detail['ygd']];
        $light_config[] = ['近光灯', $detail['jgd']];
        $light_config[] = ['LED日间行车灯', $detail['ledrjxcd']];
        $light_config[] = ['自适应远近光', $detail['zsyyjg']];
        $light_config[] = ['自动头灯', $detail['zdtd']];
        $light_config[] = ['转向辅助灯', $detail['zxfzd']];
        $light_config[] = ['转向头灯', $detail['zxtd']];
        $light_config[] = ['前雾灯', $detail['qwd']];
        $light_config[] = ['大灯高度可调', $detail['ddgdkt']];
        $light_config[] = ['大灯清洗装置', $detail['ddqxzz']];
        $light_config[] = ['大灯清洗装置', $detail['ddqxzz']];
        $data['light_config'] = $light_config;

        $mirrors_config = [];
        $mirrors_config[] = ['前/后电动车窗', $detail['qddcc']];
        $mirrors_config[] = ['车窗防夹手功能', $detail['ccfjsgn']];
        $mirrors_config[] = ['后视镜电动调节', $detail['hsjddtj']];
        $mirrors_config[] = ['后视镜加热', $detail['hsjjr']];
        $mirrors_config[] = ['内/外后视镜自动防眩目', $detail['nhsjzdfxm']];
        $mirrors_config[] = ['后视镜电动折叠', $detail['hsjddzd']];
        $mirrors_config[] = ['遮阳板化妆镜', $detail['zybhzj']];
        $mirrors_config[] = ['后雨刷', $detail['hys']];
        $mirrors_config[] = ['感应雨刷', $detail['gyys']];
        $data['mirrors_config'] = $mirrors_config;

        $aircondition_config = [];
        $aircondition_config[] = ['空调控制方式', $detail['ktkzfs']];
        $aircondition_config[] = ['后排独立空调', $detail['hpdlkt']];
        $aircondition_config[] = ['后座出风口', $detail['hzcfk']];
        $aircondition_config[] = ['温度分区控制', $detail['wdfqkz']];
        $aircondition_config[] = ['车内空气调节/花粉过滤', $detail['cnkqtj']];
        $aircondition_config[] = ['车载冰箱', $detail['czbx']];
        $data['aircondition_config'] = $aircondition_config;


        $data['base_info'] = $base_info;
        $data['cszdj'] = $detail['cszdj'];
        $data['qcmc'] = $detail['qcmc'];
        $data['sf'] = $detail['sf'];
        $data['yg'] = $detail['yg'];
        $data['xsjg'] = $detail['xsjg'];
        $data['csjr'] = $detail['csjr'];
        $data['ishdcx'] = $detail['ishdcx'];
        $data['hdcx'] = $detail['hdcx'];
        $data['id'] = $detail['id'];
        $data['isNewEnergy'] = $detail['isNewEnergy'];
        // var_dump($detail);die;
        return $data;

    } 

    public function getCarsRecommend($carsData,$cityid){
         $cars = M('cars');      
         $where="type='".$carsData['type']."' and is_show=1 and carnum!='".$carsData['carnum']."'";
         $field=" * ";
         $car_data_type=$cars->field($field)->where($where)->limit(0,12)->select();
         $carnums='';
         if(count($car_data_type)<6){    
            $where="type!='".$carsData['type']."' and is_show=1";
            $field=" carnum ";
            $car_rand=$cars->field($field)->where($where)->select();
            $count=6-count($car_data_type);
            $car_rand_key=array_rand($car_rand,$count);
            $carnum_arr=[];
            foreach($car_rand_key as $v){
                $carnum_arr[]=$car_rand[$v]['carnum'];
            }
            $where_rand['carnum']=array('in',$carnum_arr);
            $field=" * ";
            $car_data_rand=$cars->field($field)->where($where_rand)->select();
            if(count($car_data_type)>0 && count($car_data_rand)>0){
                $carsDatas = array_merge($car_data_type,$car_data_rand);
            }else{
                $carsDatas = count($car_data_type)>0?$car_data_type:$car_data_rand;
            }
         }else{
             $car_rand_key=array_rand($car_data_type,6);
             foreach($car_rand_key as $v){
                $carsDatas[]=$car_data_type[$v];
             }
         }
         foreach($carsDatas as $val){
            $carnums.=$carnums?','.$val['carnum']:$val['carnum'];
         }
        //获取价格
        $price=M('price');
        $field="carnum,month_pay";
        if($carnums){
            $where="carnum in (".$carnums.") and purchase_scheme=1 and is_new=1 and city_id=".$cityid;
        }else{
            $where="carnum in ('') and purchase_scheme=1 and is_new=1 and city_id=".$cityid;
        }
        $price_infos=$price->field($field)->where($where)->select();
        foreach($price_infos as $val){
            $price_data[$val['carnum']]=$val['month_pay'];
        }
        foreach($carsDatas as $key=>$val){
            $carsDatas[$key]['month_pay']=$price_data[$val['carnum']];
        }
         return $carsDatas;
    } 
    public function getCarList(){
         $cars = M('qcgl_manage', null);      
         $where=" 1=1";
         $field=" qcmc ";
         $carsData=$cars->field($field)->where($where)->select();
         $carList=[];
         foreach($carsData as $val){
             $carList[]=$val['qcmc'];
         }
         return $carList;

    } 
    /*
        功能:按查询条件获取新车数据
    */
    public function getNewData($where, $order, $page) {
        $index = ($page-1)*self::SHOW_PAGE_COUNT;    
        
        //获取符合条件的新车信息
        $car = M('qcgl_manage', null);
        $field = "jdc_cars.carnum,jdc_cars.name,jdc_cars.details,jdc_cars.guide_price,jdc_price.month_pay";
        if ($where) {
            $where .= ' and isdel = 0';
        } else {
            $where = 'isdel = 0';
        }
        //计算最大页数
        //1.结果总数
        // $count = $car->join("jdc_price on jdc_cars.carnum=jdc_price.carnum")->where($where)->count();
        $count = $car -> where($where) -> count();
        //进行查询
        // $new_car = $car->join("jdc_price on jdc_cars.carnum=jdc_price.carnum")->field($field)->where($where)->order($order)->limit($index,self::SHOW_PAGE_COUNT)->select();
        $new_car = $car -> where($where) -> field('id, qcmc, cs, jb, cszdj, xsjg, sf, yg') ->limit($index,self::SHOW_PAGE_COUNT)->select();
        $carImageDb = M('qcgl_tp', null);
        $carImages = $carImageDb -> where(['type' => 162]) -> field('qcid, pxh, tpmc') -> order('pxh asc') -> select();
        $images = [];
        foreach ($carImages as $k => $v) {
            if (!key_exists($v['qcid'], $images)) {
                $images[$v['qcid']] = $v['tpmc'];
            }
        }
        foreach ($new_car as $k => $v) {
            $new_car[$k]['image'] = $images[$v['id']];
        }
        //最大页数
        $maxPage = ceil($count/self::SHOW_PAGE_COUNT);
        $data = array();
        $data['new_car'] = $new_car;
        $data['maxPage'] = $maxPage;
        return $data;        
    }

    public function getUserOrder($tel){
         $mode  = M('user');
         $where="tel='".$tel."'";
         $userData = $mode->where($where)->find();
         $orderModle = M('order');      
         $where_order="uid=".$userData['id']." and status>=0";
         $field="*";
         $order="create_time desc";
         $orderData=$orderModle->field($field)->where($where_order)->order($order)->find();
         
         if($orderData['status']==6){
             $order_operation = M('order_operation');
             $where_operation="orderid='".$orderData['orderid']."' and status=6";
             $field=" create_time ";
             $orderOperationData=$order_operation->field($field)->where($where_operation)->find();
             if($orderOperationData){
                  $orderData['finish_time']=date("Y-m-d H:i:s",$orderOperationData['create_time']);
             }
            

             $order_month_pay = M('order_month_pay');
             $where_pay="orderid='".$orderData['orderid']."' and status=0";
             $field=" * ";
             $order="pay_time asc";
             $orderMonthPayData=$order_month_pay->field($field)->where($where_pay)->find();
             
             if($orderMonthPayData){
                 $orderData['last_pay_time']=date("Y-m-d H:i:s",$orderMonthPayData['pay_time']);
                 if($orderMonthPayData['pay_time'] -time()<=10*24*60*60 && $orderMonthPayData['status']=='0'){
                     if($orderMonthPayData['next_pay_time'] -time()>=0){
                         $orderMonthPayData['next_pay_days']=ceil(($orderMonthPayData['pay_time'] -time())/(24*60*60));
                     }else{
                         $orderMonthPayData['next_pay_days']=0-ceil((time()-$orderMonthPayData['pay_time'])/(24*60*60));
                     }
                     $orderMonthPayData['next_pay']=$orderMonthPayData['money'];
                     $orderMonthPayData['pay_month']=$orderMonthPayData['pay_month']-1;
                     $orderMonthPayData['paymoney']=$orderMonthPayData['pay_month']*$orderMonthPayData['money'];
                     $orderMonthPayData['banknum']=substr($userData['banknum'],-4);
                     $data['orderPay']=$orderMonthPayData;
                 }
             }else{
                $orderData['last_pay_time']=''; 
             }
             
            
             
         }
        $order_operation = M('order_operation');
        $where_operation="orderid='".$orderData['orderid']."' and status=0";
        $field=" usernum ";
        $orderOperationData=$order_operation->field($field)->where($where_operation)->find();
        if($orderOperationData){
            $admin = M('admin');
            $where_admin="usernum='".$orderOperationData['usernum']."'";
            $field=" tel ";
            $admin=$admin->field($field)->where($where_admin)->find();
            if($admin){
                $orderData['tel']=$admin['tel'];
            }
                
         }

         $cars = M('qcgl_manage', null);
         $where_car="id='".$orderData['carid']."'";
        //  $field=" name,details,jdc_price,guide_price,first_pay,month_pay ";
         $field=" qcmc,xsjg,cszdj,sf,yg ";
         $carData=$cars->field($field)->where($where_car)->find();
         if($carData){
            $orderData['carname']=$carData['qcmc'];
            $orderData['guide_price']=$carData['cszdj'];
            $orderData['first_pay']=$carData['sf'];
            $orderData['month_pay']=$carData['yg'];
            $orderData['uu_price']=$orderData['uu_price'];
            $orderData['apply_refund']=$orderData['apply_refund'];
         }
        
         $carsColor = M('cars_stock');
         $where_color="id='".$orderData['stockid']."'";
         $field=" color ";
         $carColorData=$carsColor->field($field)->where($where_color)->find();
         if($carColorData){
              $orderData['color']=$carColorData['color'];
         }
         $data['order']=$orderData;
         return $data;

    } 
    public function setUserOrder($order){
         $orderModle = M('order');      
         $where_order="orderid='".$order."'";
         $orderData['status']=6;
         $orderModle->where($where_order)->save($orderData);

         $orderOperation = M('order_operation');      
         $orderOperationData['orderid']=$order;
         $orderOperationData['status']=6;
         $orderOperationData['create_time']=time();
         $orderOperation->add($orderOperationData);
    }
    public function getOrderPay($tel){
         $mode  = M('user');
         $where="tel='".$tel."'";
         $userData = $mode->where($where)->find();
         $orderPayModle = M('order_pay');      
         $where_order_pay="uid=".$userData['id']." and status = 1";
         $field="*";
         $order="id desc";
         $orderPayData=$orderPayModle->field($field)->where($where_order_pay)->order($order)->select();
         if($orderPayData){
            foreach($orderPayData as $key=>$val){
                $orderPayData[$key]["create_time"]=date("Y-m-d H:i:s",$val['create_time']);
                if($val['pay_month']>0){
                    $orderPayData[$key]["paymoney"]=$val['pay_month']*$val['money'];
                }
                $orderPayData[$key]["create_time"]=date("Y-m-d H:i:s",$val['create_time']);
            }
         }

         $order_month_pay = M('order_month_pay');
         $where_pay="orderid='".$orderData['orderid']."' and status=0";
         $field=" * ";
         $order="pay_time asc";
         $orderMonthPayData=$order_month_pay->field($field)->where($where_pay)->find();
         if($orderMonthPayData){
            $orderData['last_pay_time']=date("Y-m-d H:i:s",$orderMonthPayData['pay_time']);
            if($orderMonthPayData['pay_time'] -time()<=10*24*60*60 && $orderMonthPayData['status']=='0'){
                if($orderMonthPayData['next_pay_time'] -time()>=0){
                    $orderMonthPayData['next_pay_days']=ceil(($orderMonthPayData['pay_time'] -time())/(24*60*60));
                }else{
                    $orderMonthPayData['next_pay_days']=0-ceil((time()-$orderMonthPayData['pay_time'])/(24*60*60));
                }
                $orderMonthPayData['next_pay']=$orderMonthPayData['money'];
                $orderMonthPayData['pay_month']=$orderMonthPayData['pay_month']-1;
                $orderMonthPayData['paymoney']=$orderMonthPayData['pay_month']*$orderMonthPayData['money'];
                $orderMonthPayData['banknum']=substr($userData['banknum'],-4);
                $data['next_pay']=$nextPayData;
              }
         }else{
            $orderData['last_pay_time']=''; 
         }   
         $data['order_pay']=$orderPayData;
         return $data;

    } 
}
?>